var dao = require('./dao')
const { json } = require('express')

var articleDAL = {
    article:function(article,cb){
        var sql = 'select * from topic  where tid=?'
        var params=[article.aid]
        dao(sql,params,function(err,results){
            if (err) {
                // console.log('没查到')
                cb(err, null)
            } else {
                // console.log('查到了')
                cb(null, results)
            }
        })
    },
    author:function(author,cb){
        var sql = 'select * from user where uId=?'
        var params=[author.uid]
        // console.log(params)
        dao(sql, params, function (err, results) {
            if (err) {
                console.log('没查到')
                cb(err, null)
            } else {
                // console.log('查到了')
                cb(null, results)
            }
        }) 
    },
    comment:function(comment,cb){
        var sql = 'select * from topiccomment1 join user where topiccomment1.tId=? and user.uid=topiccomment1.uId'
        var params=[comment.aid]
        console.log('params',params)
        dao(sql, params, function (err, results) {
            if (err) {
                console.log('没查到')
                cb(err, null)
            } else {
                console.log('查到了')
                cb(null, results)
            }
        }) 
    },
    commentown:function(commentown,cb){
        var sql = 'insert into topiccomment1(tId,content,uId,tTime,likes) value(?,?,?,?,?)'
        var params=[commentown.tid,commentown.content,commentown.uid,commentown.tTime,commentown.likes]
        console.log('params',params)
        dao(sql, params, function (err, results) {
            if (err) {
                console.log('没查到')
                cb(err, null)
            } else {
                console.log('查到了')
                cb(null, results)
            }
        }) 
    },
    addFabulous:function(addFabulous,cb){
        var sql = 'update topic set likeCilck=likeCilck+1 where tid = ?'
        var params=[addFabulous.tid]
        console.log('params',params)
        dao(sql, params, function (err, results) {
            if (err) {
                console.log('没查到')
                cb(err, null)
            } else {
                console.log('查到了')
                cb(null, results)
            }
        }) 
    },
    delFabulous:function(delFabulous,cb){
        var sql = 'update topic set likeCilck=likeCilck-1 where tid = ?'
        var params=[delFabulous.tid]
        console.log('params',params)
        dao(sql, params, function (err, results) {
            if (err) {
                console.log('没查到')
                cb(err, null)
            } else {
                console.log('查到了')
                cb(null, results)
            }
        }) 
    },
}
module.exports = articleDAL